import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';

import 'loading_widget.dart';

class LoadingUtil {
  // 工厂模式
  factory LoadingUtil() => _getInstance();

  static LoadingUtil get instance => _getInstance();
  static LoadingUtil _instance;

  LoadingUtil._internal();

  static LoadingUtil _getInstance() {
    if (_instance == null) {
      _instance = new LoadingUtil._internal();
    }
    return _instance;
  }

  OverlayEntry _entry;

  show() {
    if (_entry == null) {
      _entry = OverlayEntry(
        builder: (context) => LoadingWidget(),
      );

      Overlay.of(GlobalPageContext.instance.context).insert(_entry);
    }
  }

  hide() {
    if (_entry != null) {
      _entry.remove();
      _entry = null;
    }
  }
}
